import request from "@/utils/request";

/**
 * spu列表每一项的类型
 */
export interface spuInfoItemType {
  id?: number; //当前数据id
  spuName: string; //当前spu名称
  description: string; //当前spu详情
  category3Id: number | null; //当前spu所在的3级分类的3id
  tmId: number | null; //当前spu所在的品牌
  spuSaleAttrList: null | saleAttrListType; //当前spu的销售属性列表
  spuImageList: null | saleImageListType; //当前spu的图片列表
}
//spu列表类型
export type spuInfoListType = spuInfoItemType[];
//spu信息类型
export interface spuInfoType {
  records: spuInfoListType;
  total: number;
  size: number;
  current: number;
  searchCount: boolean;
  pages: number;
}
// !!!spu列表每一项的类型结束
/**
 * 销售属性列表类型
 */
export interface baseSaleItemType {
  id: number;
  name: string;
}

export type baseSaleListType = baseSaleItemType[];
// !!!销售属性列表类型结束
/**
 * spu图片类型
 */
export interface saleImageItemType {
  id?: number;
  spuId?: number;
  imgName?: string; // 当前图片名称
  imgUrl?: string; // 当前图片地址
  name?: string;
  url?: string;
  isDefault?: boolean;
  isSelect?: boolean;
}
export type saleImageListType = saleImageItemType[];
// !!!spu图片类型结束

/**
 * 销售属性列表类型
 */
// 销售属性列表每个属性的类型
export interface saleAttrValueItemType {
  id?: number;
  spuId?: number;
  baseSaleAttrId: number;
  saleAttrValueName: string;
  saleAttrName?: string;
  isChecked?: null;
}
export type saleAttrValueListType = saleAttrValueItemType[];
// 销售属性列表每一项
export interface saleAttrItemType {
  id?: number;
  spuId?: number;
  baseSaleAttrId: number;
  saleAttrName: string;
  isEdit?: boolean;
  iptValue?: string;
  spuSaleAttrValueList: saleAttrValueListType;
  saleAttrIdValueId?: string;
}

export type saleAttrListType = saleAttrItemType[];
// !!!销售属性列表类型结束

//sku平台属性每一项的类型
export interface skuAttrItemType {
  attrId: number; //当前平台属性的id
  valueId: number; //当前平台属性的选中的值的id
  attrName?: string;
  id?: number;
  skuId?: number;
  valueName?: string;
}
//sku平台属性列表类型
export type skuAttrListType = skuAttrItemType[];

//sku图片列表
export interface skuImageItemType {
  id?: number;
  imgName: string; //图片名称
  imgUrl: string; //图片地址
  isDefault: string; //是否默认
  spuImgId: number; //当前image的id
  skuId?: number;
}
export type skuImageListType = skuImageItemType[];

//sku销售属性列表
export interface skuSaleAttrItemType {
  saleAttrId: number; //当前销售属性的id
  saleAttrValueId: number; //当前销售属性选中的值
  id?: number;
  saleAttrName?: string;
  saleAttrValueName?: string;
  skuId?: number;
  spuId?: number;
}
export type skuSaleAttrListType = skuSaleAttrItemType[];

//新增sku的时候 参数的类型
export interface skuInfoType {
  category3Id: number; //三级分类id
  createTime?: string; //创建时间
  id?: number; //本身id
  isSale: 0 | 1; //是否上架
  price: number; //价格
  skuDefaultImg: string; //默认图片
  skuDesc: string; //描述
  skuName: string; //名称
  spuId: number; //当前所在的spu的id
  tmId: number; //当前所在的品牌id
  weight: string; //重量
  skuAttrValueList: skuAttrListType;
  skuImageList: skuImageListType;
  skuSaleAttrValueList: skuSaleAttrListType;
}

// 获取sku列表类型
export type skuListType = skuInfoType[];
/**
 * 1.请求spu列表
 * @param page
 * @param limit
 * @param category3Id
 * @returns
 */
export const reqSpuInfo = (
  page: number,
  limit: number,
  category3Id: number
) => {
  return request.get<null, spuInfoType>(
    `/admin/product/${page}/${limit}?category3Id=${category3Id}`
  );
};

/**
 * 2.请求获取所有的销售属性列表
 * @returns
 */
export const reqBaseSaleList = () => {
  return request.get<null, baseSaleListType>(`/admin/product/baseSaleAttrList`);
};

/**
 * 3.请求获取spu图片列表
 * @param spuId
 * @returns
 */
export const reqSaleImageList = (spuId: number) => {
  return request.get<null, saleImageListType>(
    `/admin/product/spuImageList/${spuId}`
  );
};

/**
 * 4.请求获取销售属性列表
 * @param spuId
 * @returns
 */
export const reqSaleAttrList = (spuId: number) => {
  return request.get<null, saleAttrListType>(
    `/admin/product/spuSaleAttrList/${spuId}`
  );
};

/**
 * 5.请求新增销售属性
 * @param spuInfo
 * @returns
 */
export const reqAddspu = (spuInfo: spuInfoItemType) => {
  return request.post<null, null>(`/admin/product/saveSpuInfo`, spuInfo);
};

/**
 * 6.请求修改销售属性
 * @param spuInfo
 * @returns
 */
export const reqUpdatespu = (spuInfo: spuInfoItemType) => {
  return request.post<null, null>(`/admin/product/updateSpuInfo`, spuInfo);
};

/**
 * 7.请求删除spu
 * @param spuId
 * @returns
 */
export const reqDeleteSpu = (spuId: number) => {
  return request.delete<null, null>(`/admin/product/deleteSpu/${spuId}`);
};

/**
 * 8.新增sku
 * @param skuInfo
 * @returns
 */
export const reqAddSku = (skuInfo: skuInfoType) => {
  return request.post<null, null>(`/admin/product/saveSkuInfo`, skuInfo);
};

/**
 * 9.获取sku列表
 * @param spuId
 * @returns
 */
export const reqGetSkuList = (spuId: number) => {
  return request.get<null, skuListType>(`/admin/product/findBySpuId/${spuId}`);
};
